package com.wowangz.cms.dailyoperation.messageswitch.service;

import java.util.List;
import java.util.Map;

import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.dailyoperation.messageswitch.entity.MessageSwitch;
import com.wowangz.cms.dailyoperation.store.pojo.StoreEntity;
import com.wowangz.cms.dailyoperation.store.pojo.StoreRedisInfo;
import com.wowangz.cms.syscore.exception.ServiceException;
import com.wowangz.cms.syscore.service.BaseService;
import com.wowangz.cms.syscore.utils.JedisUtil;

public class MessageSwitchService extends BaseService{

	/**
	 * 分页查询总条数
	 * @param map
	 * @return
	 */
	public long findMessageSwitchStoreForListResultCount(Map<String, Object> map) throws ServiceException {
		try {
			return getSqlSession().selectOne("messageSwitch.queryMessageSwitchStoreForListCount", map);
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}
	/**
	 * 查询分页的记录数目
	 * @param map
	 * @return
	 */
	public List<StoreEntity> findMessageSwitchStoreForListResultList(Map<String, Object> map) {
		try {
			return getSqlSession().selectList("messageSwitch.queryMessageSwitchStoreForListPage", map);
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}
	/**
	 * 分页查询总条数
	 * @param map
	 * @return
	 */
	public long findMessageSwitchForListResultCount(Map<String, Object> map) throws ServiceException {
		try {
			return getSqlSession().selectOne("messageSwitch.queryMessageSwitchForListCount", map);
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}
	/**
	 * 查询分页的记录数目
	 * @param map
	 * @return
	 */
	public List<MessageSwitch> findMessageSwitchForListResultList(Map<String, Object> map) {
		try {
			return getSqlSession().selectList("messageSwitch.queryMessageSwitchForListPage", map);
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}
	public StoreEntity queryStoreInfo(Map<String, Object> map) throws ServiceException{
		try {
			return getSqlSession().selectOne("messageSwitch.queryMessageSwitchForStoreInfo", map);
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}
	/******
	 * 保存数据
	 * @param messageSwitch
	 * @throws ServiceException
	 */
	public void insertMessageSwitch(MessageSwitch messageSwitch) throws ServiceException {
		try {
			getSqlSession().insert("messageSwitch.insertMessageSwitch", messageSwitch);
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}
	/************
	 * 查询商户 开通的短信
	 * @param map
	 * @return
	 */
	public List<MessageSwitch> queryMessageSwitchForList(Map<String, Object> map) {
		try {
			return getSqlSession().selectList("messageSwitch.queryMessageSwitchForList", map);
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}
	/********
	 * 修改
	 * @param messageSwitch
	 * @throws ServiceException
	 */
	public void updateMessageSwitch(MessageSwitch messageSwitch) throws ServiceException {
		try {
			getSqlSession().update("messageSwitch.updateMessageSwitch", messageSwitch);
			setMessageSwitchInfoToRedis(messageSwitch);
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}
	/***********
	 * 重新设置商户信息中 商户子开关
	 * @param messageSwitch
	 * @throws ServiceException
	 */
	private void setMessageSwitchInfoToRedis(MessageSwitch messageSwitch)throws ServiceException{
		try {
			//1.从redis获得对应的商户信息 
			StoreRedisInfo storeInfo = (StoreRedisInfo) JedisUtil.getPublicDataObject(messageSwitch.getStoreid().toString(),
					StoreRedisInfo.class, JedisUtil.STORE_INFO);
			//2.重新设置商户对应的 子开关信息
			if(messageSwitch.getSmstype()!=null && messageSwitch.getSmstype()==SysConstant.MessageSwitchType.LOGIN_SWITCH){
				//登录短信
				storeInfo.setLoginswitch(String.valueOf(messageSwitch.getIsswitch()));
			}else if(messageSwitch.getSmstype()!=null && messageSwitch.getSmstype()==SysConstant.MessageSwitchType.INSTRUCTION_SWITCH){
				//指令短信
				storeInfo.setInstructionswitch(String.valueOf(messageSwitch.getIsswitch()));
			}else if(messageSwitch.getSmstype()!=null && messageSwitch.getSmstype()==SysConstant.MessageSwitchType.AGREENT_SWITCH){
				//协议短信
				storeInfo.setAgreementswitch(String.valueOf(messageSwitch.getIsswitch()));
			}else if(messageSwitch.getSmstype()!=null && messageSwitch.getSmstype()==SysConstant.MessageSwitchType.SERVICE_SWITCH){
				//邮件
				storeInfo.setServiceswitch(String.valueOf(messageSwitch.getIsswitch()));
			}
			JedisUtil.insertPublicDataObject(storeInfo.getStoreid().toString(), storeInfo, JedisUtil.STORE_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}
}
